package com.xpec.cipher.handler;

import com.xpec.cipher.CipherConnectionConstant;
import com.xpec.cipher.CipherConnectionStatus;
import com.xpec.cipher.CipherHandler;
import com.xpec.cipher.tals.TalsConstance;

/**
 * Client端用的 HandShakeHandler
 * @author linyuntai
 */
public class ClientHandShakeHandler implements CipherConnectionPacketHandler
{

	@Override
	public void receiveHandle(CipherHandler cipherHandler) {
		
		// 接收到的是server发过来的HandShake包
		int contentType = cipherHandler.readUnsignedByteFromRealContent();
		if (contentType != TalsConstance.CONTENT_TYPE_HAND_SHAKE){
			return;
		}
		/*int handShakeType = cipherHandler.readUnsignedByteFromRealContent();
		if (handShakeType != TalsConstance.HAND_SHAKE_TYPE_SERVER_HELLO)
		{
			return;
		}*/
		
		// 读取 server hello
		byte[] serverRandom = new byte[TalsConstance.SERVER_HELLO_SIZE];
		cipherHandler.readByteFromRealContent(serverRandom);
		
		// 记录一下server的随机数
		cipherHandler.setParameter(CipherConnectionConstant.PARA_KEY_SERVER_RANDOM,
			serverRandom);
		
		// 进入到第二种状态中，交换密钥!
		/*cipherHandler.setParameter(CipherConnectionConstant.PARA_KEY_STATUS,
				CipherConnectionStatus.CLIENT_CHANGE_CIPHER);*/
		
		//2015/10/23 by robert 修改成只有兩個狀態
		cipherHandler.setParameter(CipherConnectionConstant.PARA_KEY_STATUS,
				CipherConnectionStatus.FINISH);
	}

	@Override
	public void sendHandle(CipherHandler cipherHandler) 
	{
		cipherHandler.passMessage(CipherHandler.PASS_MESSAGE_SEND);
	}

}
